<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Models\Service;
use Encore\Admin\Form;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Grid;
use App\Models\Category;

class ServiceController extends Controller
{
    protected $name = '需求管理';
    use ModelForm;


    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->name);
            $content->description($this->name);


            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     *
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->name);
            $content->description('编辑');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->name);
            $content->description('创建');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Service::class, function (Grid $grid) {
            $grid->model()->orderBy('id', 'DESC');

            $grid->filter(function($filter){
                $filter->disableIdFilter();
                $filter->like('title', '标题');
                //$filter->addFilter($filter->like('post_title', '标题'));
                $filter->equal('status', '类型')->select(Service::$status);

                $filter->equal('post_type','栏目')->select(function(){
                    $temp = Category::menu(Service::$catid);
                    $ret = array();
                    foreach ( $temp as $row )
                    {
                        $ret[$row['id']] = $row['name'];
                        if(isset($row['child']))
                        {
                            foreach ($row['child'] as $r)
                            {
                                $ret[$r['id']] = '--'.$r['name'];
                            }
                        }
                    }
                    return $ret;
                });

            });
            $grid->column('id', 'ID')->sortable();
            $grid->column('title', '标题');
            $grid->column('category.name', '栏目');

            $states = [
                'on'  => ['value'=>1,'text' => '已审核', 'color' => 'primary'],
                'off' => ['value'=>0,'text' => '未审核', 'color' => 'default'],
            ];
            $grid->column('status', '状态')->switch($states);


            $grid->column('updated_at', '更新时间');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {

        return Admin::form(Service::class, function (Form $form) {

            $form->number('id', 'ID')->attribute('disabled', false);
            $form->text('title', '标题');

            $form->editor('content', '内容');
            $form->textarea('remark', '备注');
            $form->select('type', '栏目')->rules(function ($form) {
                return 'required|numeric|min:0';
            })->options(function() {
                $temp = Category::menu(Service::$catid);
                $ret = array();
                foreach ( $temp as $row )
                {
                    $ret[$row['id']] = $row['name'];
                    if(isset($row['child']))
                    {
                        foreach ($row['child'] as $r)
                        {
                            $ret[$r['id']] = '----'.$r['name'];
                        }
                    }
                }
                return $ret;
            })->default('3');
            $form->radio('status','审核状态')->options(['1' => '是', '0'=> '否']);
            $form->saving(function($form){
                if($form->status == 'on')
                {
                    $form->status = 1;
                }else{
                    $form->status = 0;
                }
            });
        });
    }
}
